import React, { Component } from 'react'
import {Product} from '../types/products'
import {connect} from 'react-redux'

interface IProps{
    item:Product,
    addCar:(pro:Product) => void
}

class ProductItem extends Component<IProps>{
    render() {
        let {title,price,id} = this.props.item;
        return (
            <div>
                <h4>{title}</h4>
                <span>价格：{price}</span>
                <button onClick={() => {this.props.addCar(this.props.item)}}>加入购物车</button>
            </div>
        )
    }
}

let stateMapToProps = (state:any) => {
    return {}
}

let dispatchMapToProps = (dispatch:any) => {
    return {
        addCar(pro:Product){
            dispatch({type:'ADD_PRO',pro})
        }
    }
}

export default connect(stateMapToProps,dispatchMapToProps)(ProductItem)
